package com.xi.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xi.mapper.BookMapper;
import com.xi.mapper.BorrowMapper;
import com.xi.mapper.ReservationMapper;
import com.xi.mapper.UserMapper;
import com.xi.pojo.*;
import com.xi.service.BookService;
import com.xi.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private UserMapper userMapper;

    /*
     * 分页查询
     * */
    @Override
    public PageResult page(BookQueryParam bookQueryParam) {
        // 1.设置分页参数
        PageHelper.startPage(bookQueryParam.getPage(), bookQueryParam.getPageSize());

        // 2.执行查询
        List<Book> rows = bookMapper.list(bookQueryParam);

        // 3.解析查询结果，并封装
        Page<Book> p = (Page<Book>) rows;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    /*
     * 书籍添加
     * */
    @Override
    public void add(Book book) {
        //将书籍的参数补齐
        book.setStatus(book.getStock() > 0 ? 1 : 0);

        //更新更新时间
        book.setUpdateTime(LocalDateTime.now());

        //添加书籍
        bookMapper.add(book);
    }

    /*
     * 书籍删除
     * */
    @Override
    public void deleteById(Integer id) {
        bookMapper.deleteById(id);
    }

    /*
     * 书籍信息修改
     * 需要注意，当书籍被更改库存时，若库存不为零后，那么将预约表中的相关信息删除
     * */
    @Override
    public void update(Book book) {
        //修改更新时间
        book.setUpdateTime(LocalDateTime.now());

        //获取库存数
        int stock = book.getStock();

        //获取得到预约本书的所有信息
        List<Reservation> list = reservationMapper.getByBookId(book.getId());

        if (list == null || list.isEmpty()) {
            book.setStatus(stock > 0 ? 1 : 0);

            //修改书籍信息
            bookMapper.update(book);
            return;
        }

        //获取预约本书的预约数
        int count = list.size();

        //创建借阅记录集合
        List<Borrow> bList = new ArrayList<>();

        if (stock > 0 && stock - count > 0) {
            //将库存为零的图书的预约表信息删除
            //这里的库存数量大于0，并且库存减去预约数大于0，那么说明库存不为零，那么将预约表中的相关信息删除
            //通过预约id进行删除个数
            reservationMapper.deleteById(list, count);

            //添加借阅记录
            SystemConfig sc = userMapper.getSystemConfig();
            Integer renewalDays = sc.getRenewalDays();
            for (Reservation reservation : list) {
                Borrow  borrow = new Borrow();
                borrow.setUserId(reservation.getUserId());
                borrow.setBookId(reservation.getBookId());
                borrow.setBorrowDays(renewalDays);
                borrow.setBorrowingTime(LocalDateTime.now());
                borrow.setReturnTime(LocalDateTime.now().plusDays(renewalDays));
                borrow.setStatus(1);

                bList.add(borrow);
            }

            book.setStatus(1);

            stock = stock - count;
        } else if (stock > 0 && stock - count <= 0) {
            //这里的库存数量大于0，并且库存减去预约数小于0，那么说明库存为零，那么将预约表中的相关信息删除
            //通过预约id进行删除个数
            reservationMapper.deleteById(list, stock);

            //添加借阅记录
            SystemConfig sc = userMapper.getSystemConfig();
            Integer renewalDays = sc.getRenewalDays();
            for (int i = 0; i < stock; i++) {
                Borrow  borrow = new Borrow();
                borrow.setUserId(list.get(i).getUserId());
                borrow.setBookId(list.get(i).getBookId());
                borrow.setBorrowDays(renewalDays);
                borrow.setBorrowingTime(LocalDateTime.now());
                borrow.setReturnTime(LocalDateTime.now().plusDays(renewalDays));
                borrow.setStatus(1);

                bList.add(borrow);
            }

            book.setStatus(0);

            stock = 0;
        }

        //给数据库中添加借阅记录
        borrowMapper.add(bList);

        book.setStock(stock);

        //修改书籍信息
        bookMapper.update(book);
    }

    /*
     * 获取所有分类
     * */
    @Override
    public Object listClassify() {
        List<String> list = bookMapper.listClassify();
        return list;
    }

    /*
     * 获取分类统计
     * */
    @Override
    public List<BookTypeCount> listTypeCount() {
        return bookMapper.listTypeCount();
    }
}
